//
//  DKAuthHomeLimitCell.m
//  DanKU
//
//  Created by 吴斌 on 2023/11/9.
//

#import "DKAuthHomeLimitCell.h"
static NSString * DKAuthHomeLimitCellId = @"DKAuthHomeLimitCellId";
@interface DKAuthHomeLimitCell()
@property (nonatomic, strong)UIImageView * authHomeLimitProcessImageView;
@property (nonatomic, strong)UILabel * authHomeLimitStartLab;
@property (nonatomic, strong)UILabel * authHomeLimitEndLab;
@property (nonatomic, strong)UIImageView * authHomeLimitCenterImageView;
@property (nonatomic, strong)UILabel * authHomeLimitCurrentLab;
@end
@implementation DKAuthHomeLimitCell
+ (instancetype)authHomeLimitCell:(UITableView *)tableView{
    DKAuthHomeLimitCell * dk_AuthHomeLimitCell = [tableView dequeueReusableCellWithIdentifier:DKAuthHomeLimitCellId];
    if (!dk_AuthHomeLimitCell) {
        dk_AuthHomeLimitCell = [[DKAuthHomeLimitCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DKAuthHomeLimitCellId];
    }
    dk_AuthHomeLimitCell.backgroundColor = [UIColor clearColor];
    dk_AuthHomeLimitCell.selectionStyle = UITableViewCellSelectionStyleNone;
    dk_AuthHomeLimitCell.accessoryType = UITableViewCellAccessoryNone;
    return dk_AuthHomeLimitCell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self dk_addSubView];
    }
    return self;
}
- (void)dk_addSubView{
    UILabel * dk_authHomeLimitHeadLab = [[UILabel alloc] init];
    dk_authHomeLimitHeadLab.textColor = DK_Hex(@"#333333");
    dk_authHomeLimitHeadLab.font = DK_FONT_Medium(15);
    dk_authHomeLimitHeadLab.text = @"Use my limit";
    [self addSubview:dk_authHomeLimitHeadLab];
    [dk_authHomeLimitHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(15)));
        make.top.equalTo(@(DK_IsWidthScale(10)));
        make.height.equalTo(@(DK_IsWidthScale(21)));
    }];
    UIView *dk_authHomeLimitBgView = [[UIView alloc] init];
    dk_authHomeLimitBgView.backgroundColor = UIColor.whiteColor;
    dk_authHomeLimitBgView.layer.cornerRadius = DK_IsWidthScale(15);
    dk_authHomeLimitBgView.clipsToBounds = YES;
    [self.contentView addSubview:dk_authHomeLimitBgView];
    [dk_authHomeLimitBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(15)));
        make.right.equalTo(@(-DK_IsWidthScale(15)));
        make.top.equalTo(@(DK_IsWidthScale(41)));
        make.height.equalTo(@(DK_IsWidthScale(60)));
    }];
    self.authHomeLimitProcessImageView = [[UIImageView alloc] init];
    self.authHomeLimitProcessImageView.userInteractionEnabled = YES;
    self.authHomeLimitProcessImageView.image = DK_GetImage(@"dk_HomeAuthorizeLimitProcessBg");
    [dk_authHomeLimitBgView addSubview:self.authHomeLimitProcessImageView];
    [self.authHomeLimitProcessImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(0)));
        make.bottom.equalTo(@(DK_IsWidthScale(0)));
        make.width.equalTo(@(DK_IsWidthScale(0)));
    }];
    self.authHomeLimitCurrentLab = [[UILabel alloc] init];
    self.authHomeLimitCurrentLab.textColor = DK_Hex(@"#4566FF");
    self.authHomeLimitCurrentLab.font = DK_FONT_Regular(20);
    self.authHomeLimitCurrentLab.text = @"₹0";
    [dk_authHomeLimitBgView addSubview:self.authHomeLimitCurrentLab];
    [self.authHomeLimitCurrentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(dk_authHomeLimitBgView);
        make.top.equalTo(@(DK_IsWidthScale(16)));
        make.height.equalTo(@(DK_IsWidthScale(28)));
    }];
    //刻度
    for (int i = 0; i < 16; i++) {
        UIView *dk_authHomeLimitLineView = [[UIView alloc] init];
        dk_authHomeLimitLineView.backgroundColor = DK_Hex(@"#4566FF");
        [dk_authHomeLimitBgView addSubview:dk_authHomeLimitLineView];
        dk_authHomeLimitLineView.tag = 100+i;
        [dk_authHomeLimitLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(DK_IsWidthScale(15)+DK_IsWidthScale(21)*i));
            make.bottom.equalTo(@(DK_IsWidthScale(0)));
            make.height.equalTo(@(DK_IsWidthScale(6)));
            make.width.equalTo(@(1));
        }];
    }
    //开始
    UIView *dk_authHomeLimitLineView = [dk_authHomeLimitBgView viewWithTag:100];
    
    self.authHomeLimitStartLab = [[UILabel alloc] init];
    self.authHomeLimitStartLab.textColor = DK_Hex(@"#4566FF");
    self.authHomeLimitStartLab.font = DK_FONT_Regular(14);
    self.authHomeLimitStartLab.text = @"₹0";
    [self.contentView addSubview:self.authHomeLimitStartLab];
    [self.authHomeLimitStartLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(dk_authHomeLimitLineView);
        make.top.equalTo(dk_authHomeLimitBgView.mas_bottom).offset(DK_IsWidthScale(5));
        make.height.equalTo(@(DK_IsWidthScale(20)));
    }];
    self.authHomeLimitEndLab = [[UILabel alloc] init];
    self.authHomeLimitEndLab.textColor = DK_Hex(@"#4566FF");
    self.authHomeLimitEndLab.font = DK_FONT_Regular(14);
    self.authHomeLimitEndLab.text = @"₹0";
    [self.contentView addSubview:self.authHomeLimitEndLab];
    [self.authHomeLimitEndLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-DK_IsWidthScale(16)));
        make.top.equalTo(dk_authHomeLimitBgView.mas_bottom).offset(DK_IsWidthScale(5));
        make.height.equalTo(@(DK_IsWidthScale(20)));
    }];
    self.authHomeLimitCenterImageView = [[UIImageView alloc] init];
    self.authHomeLimitCenterImageView.userInteractionEnabled = YES;
    self.authHomeLimitCenterImageView.image = DK_GetImage(@"dk_HomeAuthorizeLimitCenterBg");
    [self.contentView addSubview:self.authHomeLimitCenterImageView];
    [self.authHomeLimitCenterImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(19)));
        make.top.equalTo(@(DK_IsWidthScale(91)));
        make.width.equalTo(@(DK_IsWidthScale(24)));
        make.height.equalTo(@(DK_IsWidthScale(24)));
    }];
    UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panLimitGes:)];
    [self.authHomeLimitCenterImageView addGestureRecognizer:panGes];
}
- (void)updataHomeLimitData{
    DKUserComplaintModel *dk_UserComplaintMdl = [DKGlobalHelper defaultManager].userComplaintMdl;
    if(dk_UserComplaintMdl){
        self.authHomeLimitEndLab.text = [NSString stringWithFormat:@"₹%@",[NSString makeShowMoney:dk_UserComplaintMdl.unused]];
//        self.authHomeLimitCurrentLab.text = [NSString stringWithFormat:@"₹0",[NSString makeShowMoney:dk_UserComplaintMdl.used]];
        self.authHomeLimitCurrentLab.text = [NSString stringWithFormat:@"₹%@",[NSString makeShowMoney:dk_UserComplaintMdl.unused]];
        [self.authHomeLimitCenterImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(DK_IsWidthScale(19)+DK_IsWidthScale(21)*15));
        }];
        [self.authHomeLimitProcessImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(DK_IsWidthScale(15)+DK_IsWidthScale(21)*15));
        }];
    }
}
- (void)panLimitGes:(UIPanGestureRecognizer*)panGes{
    CGPoint transP = [panGes translationInView:self.contentView];
    CGFloat move = self.authHomeLimitCenterImageView.left + transP.x;
    if(move <= DK_IsWidthScale(19)){
        move = DK_IsWidthScale(19);
    }
    if(move >= (DK_IsWidthScale(19)+DK_IsWidthScale(21)*15)){
        move = DK_IsWidthScale(19)+DK_IsWidthScale(21)*15;
    }
    [self.authHomeLimitCenterImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(move));
    }];
    [self.authHomeLimitProcessImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(move - DK_IsWidthScale(4)));
    }];
    DKUserComplaintModel *dk_UserComplaintMdl = [DKGlobalHelper defaultManager].userComplaintMdl;
    
    CGFloat allCount = DK_IsWidthScale(21)*15.0;
    CGFloat margeCount = allCount + DK_IsWidthScale(19) - move;
    CGFloat margeRate = margeCount/allCount;
    CGFloat marge = (1 - margeRate);
    int current = (int)(marge * [dk_UserComplaintMdl.unused intValue]/100) *100;
    self.authHomeLimitCurrentLab.text = [NSString stringWithFormat:@"₹%@",[NSString makeShowMoney:[NSString stringWithFormat:@"%d",(int)ceil(current)]]];
    // 复位,表示相对上一次
    [panGes setTranslation:CGPointZero inView:self.contentView];
    if(self.AuthHomeLimitBlock){
        self.AuthHomeLimitBlock([NSString stringWithFormat:@"%d",(int)ceil(current)]);
    }
}
@end
